
require_relative "https"

module Google

	GOOGLE_CLIENT_LOGIN_URL = URI.parse('https://www.google.com/accounts/ClientLogin')



	class Client
		
		CLIENT_SOURCE = 'Google-API-Ruby'

		def login(email, password, service)
			
			https = HTTPS_Ruby_1_9.new(GOOGLE_CLIENT_LOGIN_URL.host, GOOGLE_CLIENT_LOGIN_URL.port)

			@auth = https.start do |https|
				request = Net::HTTP::Post.new(GOOGLE_CLIENT_LOGIN_URL.path)  
				request.set_form_data({'Email' => email, 'Passwd' => password, 'service' => service, 'source' => CLIENT_SOURCE },'&')  
				response = https.request(request)

				if response.kind_of? Net::HTTPSuccess
					auth_line = nil
					response.body.each_line { |line| 
						auth_line = line if line.start_with?('Auth=') 
					}
					auth_line[5 .. -1].strip! if auth_line
				end #if
			end
		end

		

		private
		def connection(url)
			conn = Net::HTTP.new(url.host, 80)
		end

		public
		def get(url)

			response = connection(url).start do |http|  
				response = http.send_request('GET', url.path, "", {'GData-Version' => "2", 
					'Authorization' => "GoogleLogin auth=#{@auth}",
					'Content-Type'  => 'application/atom+xml'
				})
			end
			p response.body
		end #get
		
		 
		
	end #Class Client
end


def _test_google_client
	client = Google::Client.new
	file = File.new('./password.txt')
	password = file.readline
	file.close
	client.login('healer.kx.yu@gmail.com', password)
end

#_test_google_client()
